/**
 * 
 */
package com.gragra.sampling.binary;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import com.gragra.sampling.PopulationProbabilityAssigner;
import com.gragra.sampling.PopulationSamplingStructure;
/**
 * @author Christoph Teichmann
 * 15.09.2013
 */
public class ToStringConverter implements ConvertSample
{
	private final int threads;
	/**
	 * @param threads
	 */
	public ToStringConverter(int threads)
	{
		super();
		this.threads = threads;
	}
	/**
	 * 
	 */
	private String best = null;
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#getBest(com.gragra.sampling.binary.BinarySplitGibbs, com.gragra.sampling.PopulationProbabilityAssigner)
	 */
	@Override
	public String getBest(PopulationSamplingStructure str, PopulationProbabilityAssigner pa)
	{return best;}
	/**
	 * 
	 */
	private Object2IntOpenHashMap<String> counts = null;
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#minRisk(com.gragra.sampling.binary.BinarySplitGibbs, com.gragra.sampling.PopulationProbabilityAssigner)
	 */
	@Override
	public String minRisk(PopulationSamplingStructure str, PopulationProbabilityAssigner pa)
	{
		String best = null;
		int am = -1;
		for(String s : this.counts.keySet())
		{
			int val = this.counts.getInt(s);
			if(val > am)
			{
				am = val;
				best = s;
			}
		}
		return best;
	}
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#addSample(com.gragra.sampling.binary.BinarySplitGibbs)
	 */
	@Override
	public void addSample(PopulationSamplingStructure str)
	{
		ThreadedToString tts = (ThreadedToString) str;
		if(this.counts == null)
		{this.counts = new Object2IntOpenHashMap<String>();}
		for(int i=0;i<threads;++i)
		{counts.addTo(tts.toString(i), 1);}
	}
	/* (non-Javadoc)
	 * @see com.gragra.sampling.binary.ConvertSample#setBest(com.gragra.sampling.binary.BinarySplitGibbs, int)
	 */
	@Override
	public void setBest(PopulationSamplingStructure str, int thread)
	{
		ThreadedToString tts = (ThreadedToString) str;
		this.best = tts.toString(thread);
	}
	@Override
	public double getNumberSeen(String goldTwo)
	{return this.counts.getInt(goldTwo.trim());}
}